package com.app.repository.impl;

import com.app.model.main.Favorite;
import com.app.repository.interfaces.FavoriteMapper;
import com.app.support.dao.MybatisDao;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lichaojie on 2017/3/12. It's a beautiful day.
 */
@Repository
public class FavoriteMapperImpl implements FavoriteMapper {

    @Autowired
    private MybatisDao mybatisDao;

    @Override
    public int deleteByPrimaryKey(Long id){
        return mybatisDao.delete("com.app.dao.main.FavoriteMapper",".deleteByPrimaryKey",id);
    }

    @Override
    public int insert(Favorite record){
        return mybatisDao.delete("com.app.dao.main.FavoriteMapper",".insert",record);
    }

    @Override
    public int insertSelective(Favorite record){
        return mybatisDao.insert("com.app.dao.main.FavoriteMapper", ".insertSelective", record);
    }

    @Override
    public Favorite selectByPrimaryKey(Long id){
        return mybatisDao.get("com.app.dao.main.FavoriteMapper", ".selectByPrimaryKey", id);
    }

    @Override
    public Favorite selectByParam(long userId, String time){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("time",time);
        return mybatisDao.get("com.app.dao.main.FavoriteMapper",".selectByParam",map);
    }

    @Override
    public int updateByPrimaryKeySelective(Favorite record){
        return mybatisDao.update("com.app.dao.main.FavoriteMapper", ".updateByPrimaryKeySelective", record);
    }

    @Override
    public int updateByPrimaryKey(Favorite record){
        return mybatisDao.update("com.app.dao.main.FavoriteMapper", ".updateByPrimaryKey", record);
    }

    @Override
    public List<Favorite> getListOrderedByIdDESC(long userId, long lastId, int page, int number, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("lastId",lastId);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.FavoriteMapper", ".getListOrderedByIdDESC", map, new RowBounds(0, number));
    }

    @Override
    public List<Favorite> getListOrderedByIdASC(long userId, long lastId, int page, int number, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("lastId",lastId);
        map.put("type",type);
        return mybatisDao.getList("com.app.dao.main.FavoriteMapper",".getListOrderedByIdASC",map,new RowBounds(0,number));
    }

    @Override
    public long getMaxId(){
        return mybatisDao.count("com.app.dao.main.FavoriteMapper",".getMaxId",null);
    }

    @Override
    public Favorite getFlagWithParam(long userId, long objectId, byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("objectId",objectId);
        map.put("type",type);
        return mybatisDao.get("com.app.dao.main.FavoriteMapper",".getFlagWithParam",map);
    }

    @Override
    public long getFavoriteNumber(long objectId,byte type){
        Map<String,Object> map = new HashMap<>();
        map.put("objectId",objectId);
        map.put("type",type);
        return mybatisDao.count("com.app.dao.main.FavoriteMapper",".getFavoriteNumber",map);
    }

}
